// pages/goos_detail/index.js
import { request } from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList:[],
    goods_id:null
  },
  goodsInfo:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.goods_id
    this.goods_id = id
    console.log(id)
    // this.setData({
    //   goods_id:id
    // })
   this.getswiperList("/goods/detail/?goods_id="+this.goods_id)
  
  },
  getswiperList(url){
    request({ url:url})
    .then( result =>{
      console.log(result)
      this.goodsInfo = result
      this.setData({
        swiperList:result.data.message
        
      })
    })
  },
  bindpreviewImage(e){
    const urls = this.goodsInfo.data.message.pics.map(v=> v.pics_mid)
    const index = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: urls,
      current: urls[index],
      showmenu: true,
    })
  },
  bindaddcart(){
    
    const user = wx.getStorageSync("user") || [];
    if(user.nickName){
      const cart = wx.getStorageSync("cart") || [];
      console.log(cart)
      const index = cart.findIndex(v=>v.data.message.goods_id === this.goodsInfo.data.message.goods_id);
      console.log(index)
      if(index != -1){
        cart[index].num++;
        console.log(index)
      }else{
        this.goodsInfo.num = 1;
        this.goodsInfo.check=true;
        cart.push(this.goodsInfo)
        wx.setStorageSync("cart",cart);
      }
      wx.setStorageSync("cart",cart);
      wx.showToast({
        title: '加入成功',
        mask:true
      })
    }else{
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {   
          wx.setStorage({
            key: "user",
            data: res.userInfo
          })
          wx.navigateBack({
            delta: 0,
          })
          
        }
        
        
      })
    }

    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})